<?php

    class Invoicem extends CI_Model{

        var $data;

        function __construct(){
            parent::__construct();
        }

        public function get_data() {

            return $this->data;
        }

        function prepare_and_send($type,$bookid) 
        {
            if(!isset($bookid)){
                echo 'Error ocured.<br /> booking_id is not set.';
                exit();
            }
            if(!isset($type)){
                echo 'Error ocured.<br /> booking_type is not set.';
                exit();
            }

            switch ($type) {



                case 'carbooking':

                    //Get Data - informacionisistem.com
                    $post = array('carbooking_id'=>$bookid);
                    $dbRet = $this->communicator->communicate('show_invoice_rentacar', $post);

                    //print_r($dbRet);

                    //Chek if booking exist
                    $order = $this->test_response($dbRet);
                    if(!$order){
                        echo $this->getFlag().":";

                        switch($this->getFlag()){
                            case 'error':
                                echo $this->getError();
                                break;
                            case 'numrows':
                                echo $this->getNumrows();
                                break;
                            case 'info':
                                echo $this->getInfo();
                                break;

                        }
                        die();
                    }

                    //check firdst row - booking table
                    $order = $this->test_response($dbRet[0]);

                    //test for valid json
                    if($order){

                        //print_r($order[0]);
                        //print_r($order[1]);

                        $this->data['invoice_no'] = $order[0]->id;
                        $this->data['cust_firstName'] = $order[0]->firstName;
                        $this->data['cust_email'] = $order[0]->email;
                        $this->data['cust_phone'] = $order[0]->phone;

                        $this->data['invoice_title'] = $order[0]->carname;

                        $this->data['rtcr_invc_vr']  = true;

                        $this->data['titles'][0]='Pickup:';
                        $this->data['titles'][1]='Return:';

                        $this->data['datas'][0]= $order[0]->pickup_loc.' <br /> '.date('d.m.Y / H:s',$order[0]->datefrom).' h';
                        $this->data['datas'][1]= $order[0]->return_loc.' <br /> '.date('d.m.Y / H:s',$order[0]->dateto).' h';       

                        $this->data['prices'][0]=$order[0]->pickup_loc_price;
                        $this->data['prices'][1]=$order[0]->return_loc_price;

                        $this->data['price_title'] = 'Rentacar:';
                        $this->data['price_desc'] = $order[0]->carname.' - '.$order[0]->numofdays.' day(s)';
                        $this->data['price_day_price'] = $order[0]->dayprice;
                        $this->data['price_total_price'] = $order[0]->dayprice*$order[0]->numofdays;
                        $this->data['total_price'] = $order[0]->totalprice;


                        //check secon row - accessories table
                        $this->data['cb_numofdays'] = $order[0]->numofdays;

                        $order_extras = $this->test_response($dbRet[1]);

                        if($order_extras){
                            //print_r($order_extras);
                            $this->data['accesories'] = $order_extras;
                        }else{
                            //echo $this->invoice->getFlag().":";

                            switch($this->getFlag()){
                                case 'error':
                                    echo $this->getError();
                                    break;
                                case 'numrows':
                                    //echo $this->invoice->getNumrows();
                                    break;
                                case 'info':
                                    echo $this->getInfo();
                                    break;

                            }
                        }

                    }else{
                        echo 'Booking '.$this->getFlag().":";

                        switch($this->getFlag()){
                            case 'error':
                                echo $this->getError();
                                break;
                            case 'numrows':
                                echo $this->getNumrows();
                                break;
                            case 'info':
                                echo $this->getInfo();
                                break;

                        }
                    }

                    break;
                case 'excursionbooking':

                    //Get Data - informacionisistem.com
                    $post = array('excursionbooking_id'=>$bookid);
                    $dbRet = $this->communicator->communicate('show_invoice_exc', $post);

                    //Chek if booking exist
                    $order = $this->test_response($dbRet);
                    if(!$order){
                        echo $this->getFlag().":";

                        switch($this->getFlag()){
                            case 'error':
                                echo $this->getError();
                                break;
                            case 'numrows':
                                echo $this->getNumrows();
                                break;
                            case 'info':
                                echo $this->getInfo();
                                break;

                        }
                        die();
                    }
                    //data for view
                    $this->data = $order;

                    break;
                case 'tourbooking':

                    //Get Data - informacionisistem.com
                    $post = array('tourbooking_id'=>$bookid);
                    $dbRet = $this->communicator->communicate('show_invoice_tour', $post);

                    //Chek if booking exist
                    $order = $this->test_response($dbRet);
                    if(!$order){
                        echo $this->getFlag().":";

                        switch($this->getFlag()){
                            case 'error':
                                echo $this->getError();
                                break;
                            case 'numrows':
                                echo $this->getNumrows();
                                break;
                            case 'info':
                                echo $this->getInfo();
                                break;

                        }
                        die();
                    }
                    //data for view
                    $this->data = $order;

                    break;
            }

            $text = $this->load->view('site/code/showInvoice', $this->data, TRUE);

            //print_r($dbRet);
            //print_r($text);
            //exit;

            //  SEND MAIL TO CUSTOMER                 
            $this->mail_send($order[0]->email,$text);
            //$this->mail_send(,$text);

            //  SEND INFO 1 TO AGENCY
            $this->mail_send('dado@globtour.me',$text);

            //  SEND INFO 2 TO AGENCY
            $this->mail_send('info@globtour.me',$text);

            //  SEND INFO 3 TO Administrator
            $this->mail_send('ikukic@yahoo.com',$text);
            $this->mail_send('kule@t-com.me',$text);





        }

        function mail_send($to,$message){

            $from = 'no-reply@globtour.me';
            $subject = 'ONLINE BOOKING - Globtour Montenegro';

            $headers  = "From: $from\r\n";
            $headers .= "Content-type: text/html\r\n";

            return mail($to, $subject, $message, $headers); //0 or 1

        }

        private $error;
        private $info;
        private $numrows;
        private $flag;

        public function getError()
        {
            return $this->error;
        }

        public function setError($value)
        {
            $this->error = $value;
        }

        public function getInfo()
        {
            return $this->info;
        }

        public function setInfo($value)
        {
            $this->info = $value;
        }

        public function getNumrows()
        {
            return $this->numrows;
        }

        public function setNumrows($value)
        {
            $this->numrows = $value;
        }

        public function getFlag()
        {
            return $this->flag;
        }

        public function setFlag($value)
        {
            $this->flag = $value;
        }


        function test_response($dbRet){
            //print_r($dbRet);
            if(!isset($dbRet)){
                $this->setFlag('error');
                $this->setError('Rest service error!');
                return FALSE;
            };

            if(isset($dbRet->error)){
                $this->setFlag('error');
                $this->setError($dbRet->error);
                return FALSE;
            }

            if(isset($dbRet->num_rows)){
                $this->setFlag('numrows');
                $this->setNumrows($dbRet->num_rows);
                return FALSE;
            }

            if(isset($dbRet->info)){
                $this->setFlag('info');
                $this->setInfo($dbRet->info);
                return FALSE;
            }

            return $dbRet;    

        }

    }
?>
